﻿// 
// Copyright (c) 2013 Jason Bell
// 
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the 
// Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
// 

using System;
using System.Windows.Forms;

using Gharen.GLCore;

namespace ExampleInitialisation
{
    class Program
    {
        static void Main(string[] args)
        {
            var platform = Platform.Create();
            Console.WriteLine("Supported display modes:");
            foreach (DisplayMode dm in platform.DisplayModes)
                Console.WriteLine(dm);

            GLContext<GLES20> context = null;
            try
            {
                context = GLContext<GLES20>.Create();
            }
            catch (InitialisationFailedException e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
                return;
            }

            context.Window.Text = "Initialisation Example";
            context.Window.MouseDown += MouseDown;
            context.Window.MouseUp += MouseUp;
            //context.Window.MouseMove += MouseMove;
            context.Window.KeyDown += KeyDown;
            context.Window.KeyUp += KeyUp;
            context.Window.Resize += Resize;

            Console.WriteLine("OpenGL version: " + context.Version);

            var gl = context.GL;

            gl.ClearColor(0, 0, 1, 1);

            while (context.Window.IsCreated)
            {
                gl.Clear(gl.GL_COLOR_BUFFER_BIT);

                context.SwapBuffers();

                context.Window.DoEvents();
            }

            context.Dispose();
        }

        static void Resize(Window sender)
        {
            Console.WriteLine("Resize");
        }

        static void KeyUp(Window window, Keys key)
        {
            Console.WriteLine("KeyUp: " + key);
        }

        static void KeyDown(Window window, Keys key)
        {
            Console.WriteLine("KeyDown: " + key);
        }

        static void MouseMove(Window sender, int x, int y)
        {
            Console.WriteLine("MouseMove: " + x + ", " + y);
        }

        static void MouseDown(Window sender, MouseButtons buttons, int x, int y)
        {
            Console.WriteLine("MouseDown: " + buttons + " " + x + ", " + y);
        }

        static void MouseUp(Window sender, MouseButtons buttons, int x, int y)
        {
            Console.WriteLine("MouseUp: " + buttons + " " + x + ", " + y);
        }
    }
}
